<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

# INFORMATION:
# TransactionHandle.php 
# 	- this is the class that handles the connection of the transaction window to the database
#	- all MODIFY, SELECT, and DELETE functions are housed in this class
# 	
# FUNCTION INFORMATION:
#	1. CONSTRUCTOR:
#		- sets up the database connection
#	2. LOAD INFORMATION DATA:
#		a. getTransaction (transactionId) - returns all the data fields of a particular transaction
#		b. getAllTransaction (donationId) - return all the data fields of a particular donation
#	3. STORE DATA:
#		a. addNewTransaction (valuesArray, donationId) - creates a new transaction for a particular donation
# 	4. MODIFY DATA:
#		a. completeTransaction (valuesArray, transactionId) - changes the status of the transaction as complete and inputs the required information for the completion of transaction
# 


class TransactionHandle{
# FOR CLASS MANAGEMENT #
# 	The following variables are set to uppercase. This means that they are    
#	class monitoring variables.			

	# FOR DETERMINING THAT THE CLASS HAS ALREADY BEEN INSTANTIATED #
	# boolean variable [ value is NULL when class has not been instantiated ]
		private $CLASS_INSTANTIATED = NULL;							              

	# CLASS MESSAGE_HANDLE HOLDER #
	# handles the error and notification messages of the class to notify the user 
	# to capture all sql and system error, notifications and logs 
		public $MESSAGE_HANDLE = NULL;

	# DB_HANDLE INSTANCE HOLDER #
	# database connection handler [ MYSQL ]
	# instance of the database handle is placed here for the system to use the database with ease 
		public $DB_HANDLE = NULL;
		
# TRANSACTION VARIABLES #
# the ticket variables represent the data fields of the database itself
	private $donationArray;
	
# CLASS CONSTANT VARIABLES #
# this is for class management purposes
	private $errorArray = array("DB_NOTCONNECTED" => "Not connected to database.",
							    "DB_QUERYERROR" => "Problem with query",
							    "DB_INVALIDFIELD" => "Invalid field being accessed."
							   );
							  
  	# transaction statuses:
	# 0 = start
	# 1 = for pick-up
	# 2 = on-going
	# 3 = cancellation
	# 4 = complete

########################################
# TRANSACTION HANDLE CLASS CONSTRUCTOR #
########################################
	public function TransactionHandle(){
		# include the needed / required library
			require_once('DBHandle.php');
		# instantiate the DBHandle and assign it to the class attribute DB_HANDLE	
			$databaseConnection = new DBHandle();
			$this->DB_HANDLE = $databaseConnection->DB_HANDLE;
	}	

##############################	
# LOAD SYSTEM DATA FUNCTIONS #	
##############################			
	public function getTransaction ($transactionId) {
		# check if the DB_HANDLE has connected to the database
			/*if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {*/
				# protection against SQL injection, add slashes to escape all escape characters
					$transactionId = addslashes($transactionId);
				# prepare the statement
					$statement = "SELECT * FROM ticket_transaction WHERE transaction_id = '$transactionId'";
				# query & check if error has occured
					if(!$result = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'] . ' -[TransactionHandle:getTransaction]';
					}
					else {
						# debugging purposes <remove on production>	
							//echo "got it";
					
						# get the entry values
							$resultArray = mysql_fetch_array($result);
						# return the values											
							return $resultArray;
					}		
			//}
	}
	
	public function getAllTransactions ($donationId) {
		# check if the DB_HANDLE has connected to the database
			/*if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {*/
				# protection against SQL injection, add slashes to escape all escape characters
					$donationId = addslashes($donationId);
				# prepare the statement
					$statement = "SELECT transaction_id FROM ticket_transaction WHERE donation_id = '$donationId'";
				# query & check if error has occured
					if(!$result = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'] . ' -[TransactionHandle:getAllTransactions]';
					}
					else {
						# debugging purposes <remove on production>	
							//echo "got it";
					
						# get the entry values
							while ($row = mysql_fetch_assoc($result)){ 
								$resultArray[] = $row; 
							}
						# return the values											
							return $resultArray;
					}		
			//}
	}

###############################	
# STORE SYSTEM DATA FUNCTIONS #	
###############################	

	# the store functions stores the values to the class' attributes and then connects to the database to store the value
	# it is already assumed that the script that will provide the necessary input validation and error checking
	# the only security the setters provide is the addslashes() function to protect SQL injection from happening
	
	public function addNewTransaction ($valuesArray, $donationId) {
		# check if the DB_HANDLE has connected to the database
			/*if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		*/
				# prepare the statement 
					$statement = "INSERT INTO ticket_transaction (				
							donation_id,
							transaction_date,
							transaction_time,
							transaction_amountexpected,
							transaction_modeofpayment,
							transaction_status,
							transaction_notes
						) 
						VALUES (
							'{$donationId}',
							'{$valuesArray['transaction_date']}',
							'{$valuesArray['transaction_time']}',
							'{$valuesArray['transaction_amountexpected']}',
							'{$valuesArray['transaction_modeofpayment']}',
							'1',
							'{$valuesArray['transaction_notes']}'
						)";
					
				# query & check if error has occured
					if(!$result = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'] . ' -[TransactionHandle:addNewTransaction]';
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
			
						# return the values											
							return mysql_insert_id();
					}
			//}
	}
		
#########################
# MODIFY DATA FUNCTIONS #
#########################
		
	# the close donation ticket changes the status of the donation into close (which is '3' in numerical value)		
	public function completeTransaction ($valuesArray, $transactionId) {
		# check if the DB_HANDLE has connected to the database
			/*if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		*/
				$transactionId = addslashes($transactionId);
				# prepare the statement
					$statement = "UPDATE ticket_transaction SET 
						transaction_dateupdated = '{$valuesArray['transaction_dateupdated']}', 
						transaction_timeupdated = '{$valuesArray['transaction_timeupdated']}', 
						transaction_amountcollected = '{$valuesArray['transaction_amountcollected']}', 
						transaction_receipt1 = '{$valuesArray['transaction_receipt1']}',
						transaction_receipt2 = '{$valuesArray['transaction_receipt2']}', 
						transaction_status = '4', 
						transaction_notes2 = '{$valuesArray['transaction_notes2']}'					
						WHERE transaction_id = '{$transactionId}'";
				
				/*, 
								*/		
				
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'] . ' -[TransactionHandle:completeTransaction]';
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
			
						# return the values											
							return true;
					}
			//}		
	}
}	
?>
